#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
找种子
script name: find_seed
Created on 2020/2/16 19:52 by PyCharm
@author: fujianjian
"""
import json
from datetime import datetime
from threading import current_thread

import requests

from ..model.seed import Seed


class OstrichCrawler:
    __ostrich_url = 'https://www.tnbt1.xyz/api/search'

    def __init__(self) -> None:
        super().__init__()

    def crawler_seed(self, product_name=''):
        """
        请求作品的下载种子
        :param product_name:
        :return:
        """
        if product_name:
            keyword = {'source': 'idope', 'keyword': product_name, 'page': '1', 'sort': 'time'}
            try:
                res = requests.get(self.__ostrich_url, params=keyword)
                res_json = json.loads(res.text, encoding='utf-8')
                results = res_json['data']['results']
                print('ostrich result: {}'.format(results))

                seed_list = [Seed(name=ret['name'].strip(), size=ret['formatSize'].strip(),
                                  uri=ret['magnet'].strip(), hot=ret.get('hot', 0),
                                  # made_time=datetime.strptime(ret['date'], '%Y-%m-%d'),
                                  made_time=datetime.utcnow(),
                                  search_channel='ostrich', ) for ret in results]
                return seed_list

            except Exception as e:
                print('OstrichCrewler error %s' % e, current_thread().getName(), current_thread().ident)
